<?php
namespace App\Services\ThirdPayments\Recharge;

use App\Lib\PaymentMap;
use App\Lib\ThirdPartyMap;
use App\Models\RechargeOrder;
use App\Services\ThirdPayments\Contracts\Actions\HrefRedirectAction;
use App\Services\ThirdPayments\Contracts\RechargeBaseAbstract;
use App\Services\ThirdPayments\Contracts\WAPable;

class ZHYUNPayment extends RechargeBaseAbstract implements WAPable
{
    protected $gw_pay = 'https://www.aw5880.cn/pay/action';
    protected $gw_query = 'https://www.aw5880.cn/pay/select';

    public function callback(array $data)
    {
        // TODO: Implement callback() method.
        if ($this->veryCallbackSign($data)){
            return true;
        }
        return false;
    }

    public function veryCallbackSign(array $data)
    {
        // TODO: Implement veryCallbackSign() method.
        $ordno = $data["ordno"];
        $orderid = $data["orderid"];
        $price = $data["price"];
        $realprice = $data["realprice"];
        $orderuid = $data["orderuid"];
        $key = $data["key"];


        $check = md5($orderid . $orderuid . $ordno . $price . $realprice . $this->getMchKey());
        if ($key == $check) {
            return true;
        }
         return false;
    }

    public function getOrderNoFromCallback(array $data)
    {
        // TODO: Implement getOrderNoFromCallback() method.
        return $data['orderid'];
    }

    public function getOrderAmtFromCallback(array $data)
    {
        // TODO: Implement getOrderAmtFromCallback() method.
        return $data['price'];
    }

    public function getThirdNoFromCallback(array $data)
    {
        return  $data['ordno'];
    }

    /**
     * @return mixed
     */
    public function getIdentify(): string
    {
        // TODO: Implement getIdentify() method.
        return ThirdPartyMap::ZHINENGYUN;
    }

    /**
     *  needs child to achieve
     */
    public function initPaymentMap(): array
    {
        // TODO: Implement initPaymentMap() method.
        return [
            PaymentMap::WX => '20001',
            PaymentMap::ALI => '10001',
            PaymentMap::WX_WAP => '20001',
            PaymentMap::ALI_WAP => '10001',
        ];
    }

    public function query(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement query() method.
        $this->setParameter('uid', $this->getMchId());
        $this->setParameter('price', $rechargeOrder->order_amt);
        $this->setParameter('orderid', $rechargeOrder->plat_no);
        $this->setParameter('orderuid', $rechargeOrder->uid);
        $this->setParameter('key', $this->querySign());
        $res = curlHttp($this->getGwQuery(), $this->getParameters(), 'post');
        if ($res['http_code'] == 200) {
            $resArr = json_decode($res['body'], true);
            if ($resArr['code'] != 200) {
                return $resArr['msg'];
            }
            if ($resArr['status'] == 1 || $resArr['status'] == 2) {
                return true;
            }
        }
        return $res['body'];
    }

    public function bankHref(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement bankHref() method.
    }

    public function scanCode(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement scanCode() method.
        $this->initParameters($rechargeOrder);
        return $this->getGwPay() . '?' . http_build_query($this->getParameters());
    }

    public function paySign(): string
    {
        // TODO: Implement paySign() method.
        $params = $this->getParameters();
        $params['token'] = $this->getMchKey();

        ksort($params);
        $signStr = "";
        foreach ($params as $value) {
            $signStr .= $value;
        }
        return md5($signStr);
    }

    public function querySign(): string
    {
        // TODO: Implement querySign() method.
        $signStr = $this->getParameter('uid') .
            $this->getParameter('orderid') .
            $this->getParameter('price') .
            $this->getParameter('orderuid') .
            $this->getMchKey();
        return md5($signStr);
    }

    public function showSuccess(): string
    {
        // TODO: Implement showSuccess() method.
        return 'success';
    }

    public function initParameters(RechargeOrder $rechargeOrder)
    {
        $mchData = $rechargeOrder->order_data;
        // TODO: Implement initParameters() method.
        $this->setParameter('uid', $this->getMchId());
        $this->setParameter('price', $rechargeOrder->order_amt);
        $this->setParameter('istype', $this->getPaymentMap($mchData['recharge_type']));
        $this->setParameter('notify_url', $this->getCallbackUrl());
        $this->setParameter('return_url', $this->getReturnUrl());
        $this->setParameter('orderid', $rechargeOrder->plat_no);
        $this->setParameter('goodsname', array_key_exists('body', $mchData) ? $mchData['body'] : '艾尚充值订单');
        $this->setParameter('orderuid', $rechargeOrder->uid);
        $this->setParameter('key', $this->paySign());
        $this->setParameter('format', 'web');
    }

    public function wapReq(RechargeOrder $rechargeOrder)
    {
        // TODO: Implement wapReq() method.
        $this->initParameters($rechargeOrder);
        return new HrefRedirectAction($this->getGwPay() . '?' . http_build_query($this->getParameters()));
    }
}
